<?php

// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 地图区域划分ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */

quanli_require('module/base/system/mapAreaZoning.class.php');

class ajax_mapAreaZoning {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])) {

            switch ($_REQUEST['operFlg']) {

                case 1: //根据城市，查询所有地图区域
                    $this->searchMapArea();
                    break;

                case 100:

                    if (!isset($_REQUEST['act']) || !in_array($_REQUEST['act'], array( 'del', 'edit' ))) {
                        returnJsonData('参数错误');
                    }

                    if ($_REQUEST['act'] == 'edit') {
                        $this->editMapArea();
                    } else if ($_REQUEST['act'] == 'del') {
                        $this->delMapArea();
                    }

                    break;

                default:
                    returnJsonData('参数错误');
                    break;

            }

        } else {

            returnJsonData('参数错误');

        }

    }


    /**
     * 保存划分的区域名称
     */
    private function editMapArea() {

        global $G_PGLOB;

        //实例化地图区域划分类
        $mapAreaZoning = new mapAreaZoning();

        //添加地图区域
        $_REQUEST['operator'] = $G_PGLOB->currentUserID; //创建人
        $result_array = $mapAreaZoning->editMapArea( $_REQUEST ) ;

        if ($result_array['code'] == 200) {
            returnJsonData( $result_array['message'], $result_array['code'], $result_array['data'] );
        } else {
            returnJsonData( $result_array['message'], $result_array['code'] );
        }

    }


    /*
     *  根据区域信息查询所有的区域划分信息
     */
    private function searchMapArea(){
        if(!isset($_REQUEST['cityID']) || !quanli_is_int($_REQUEST['cityID'])){
            returnJsonData('参数错误');
        }

        //实例化地图区域划分类
        $mapAreaZoning = new mapAreaZoning();

        //参数组装
        $arg_array = array(
            'cityID' => $_REQUEST['cityID'],
            'useFlg' => 1,
            'brandID' => G_BRANDID_DEFAULT
        );
        $map_area_array = $mapAreaZoning->searchMapArea( $arg_array );
        if (!$map_area_array) {
            returnJsonData('没有地图区域', 404);
        }

        $return_map_area_array = array();

        foreach($map_area_array as $map_area) {

            //查询区域坐标详情
            $map_area_points_array = $mapAreaZoning->searchMapAreaPath( $map_area['areaID'] );
            $map_area['path'] = $map_area_points_array;

            $return_map_area_array[$map_area['type']][$map_area['areaID']] = $map_area;

        }

        returnJsonData('ok', 200, $return_map_area_array );

    }

    /**
     * 删除地图区域
     */
    private function delMapArea() {

        if (!isset($_REQUEST['areaID']) || !quanli_is_int($_REQUEST['areaID'])) {
            returnJsonData('参数错误');
        }

        //实例化地图区域划分类
        $mapAreaZoning = new mapAreaZoning();

        //删除地图区域
        $result_array = $mapAreaZoning->delMapArea( $_REQUEST['areaID'] );

        returnJsonData( $result_array['message'], $result_array['code'] );

    }

}
